package optionalDemo;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

class Outer {
    Nested nested;
    Nested getNested() {
        return nested;
    }
}
class Nested {
    Inner inner;
    Inner getInner() {
        return inner;
    }
}

class Inner {
    String foo;
    String getFoo() {
        return foo;
    }
}

public class OptionalTest {

    public static void ofTest() {
        //调用工厂方法创建Optional实例
        Optional<String> name = Optional.of("Sanaulla");
       name.ifPresent(System.out::println);
    }

    public static void multiTest() {
        Outer outer = new Outer();
        resolve(() -> outer.getNested().getInner().getFoo()).ifPresent(System.out::println);

        if (outer != null && outer.nested != null && outer.nested.inner != null) {
            System.out.println(outer.nested.inner.foo);
        }

        Optional.of(new Outer()).map(Outer::getNested).map(Nested::getInner).map(Inner::getFoo).ifPresent(System.out::println);

    }

    public static <T> Optional<T> resolve(Supplier<T> resolver) {
        T result = resolver.get();
        return Optional.ofNullable(result);
    }

    public static void main(String[] args) {
        multiTest();
    }

}
